<?php
namespace app\api\logic;

class Mail {
    public function _initialize()
    {}

    /**
     * 发送邮件
     * @param string $to            邮件接收者
     * @param string $subject       邮件标题
     * @param string $msgHtml       邮件内容（支持HTML）
     * @return boolean
     */
    public function sentMail($to, $subject, $msgHtml)
    {
        require_once VENDOR_PATH . 'PHPMailer/src/PHPMailer.php';
        
        $mail = new \PHPMailer\PHPMailer\PHPMailer();
        //Tell PHPMailer to use SMTP
        $mail->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        
        $mail->SMTPDebug = 0;
        //Set the hostname of the mail server
        $mail->Host = 'smtp.qq.com';
        // use
        // $mail->Host = gethostbyname('smtp.gmail.com');
        // if your network does not support SMTP over IPv6
        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = 587;
        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = 'tls';
        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;
        //Username to use for SMTP authentication - use full email address for gmail
        $mail->Username = "3001665071@qq.com";
        //Password to use for SMTP authentication
        $mail->Password = "pwfiuondbbzmdfdd";
        //Set who the message is to be sent from
        $mail->setFrom('3001665071@qq.com', 'Yang Pan');
        //Set an alternative reply-to address
        $mail->addReplyTo('3001665071@qq.com', 'Yang Pan');
        //Set who the message is to be sent to
        $mail->addAddress($to, 'Xia Yu');
        //Set the subject line
        $mail->Subject = $subject;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        // '<a href="http://wei-yong.com">微用</a>'
        $mail->msgHTML($msgHtml, dirname(__FILE__));
        //Replace the plain text body with one created manually
        //$mail->AltBody = 'This is a plain-text message body';
        //Attach an image file
        //$mail->addAttachment('images/phpmailer_mini.png');
        //send the message, check for errors
        if(! $mail->send()) {
            //echo "Mailer Error: " . $mail->ErrorInfo;
            return false;
        } else {
            return true;
        }
    }
}
